<?php
    /*
        $content['table']: contain table name
        $content['title']: contain title of table
        $content['list_label']: list label name
        $content['list_textarea']: list field that show textarea
        $content['list_dropdown']: list field that show dropdown
        $content['data_field']: data of field into table (update)
        $content['upload']: data upload
        $content['ckeditor']: list field that using Ckeditor
        $content['is_panel_area']: table is belong to panel area, yes or no?
    */
    $CI=& get_instance();
    $CI->load->model('dashboard/backend_model','be');
?>
<script type="text/javascript">
    function validateForm()
    {
        var name=document.forms['frm-insert']['name'];
        var title=document.forms['frm-insert']['title'];
        //alert(name);
        //alert(title);
        if(name != 'frm-insert')
        {
            if(name.value == null || name.value.trim() == "")    
            {
                alert('Name must be filled out'); 
                return false;
            }
        }
        else if(title != '')
        {
            if(title.value == null || title.value.trim() == "")    
            {
                alert('Title must be filled out');
                return false;
            }
        }
        return true;
    }
</script>
<div class="title">
    <h2> 
        <?=isset($content['data_field'])?'Update':'Insert'?>&nbsp;<?=$content['title']?>
    </h2>
</div>
<div class="view-ifo">
   <?php 
        if(isset($content['list_label'])):    
   ?>
       <div>
            <form name="frm-insert" method="post" action="<?=base_url('dashboard/backend/process_insert_update').'/'.$content['table']?>" onsubmit="return validateForm()" enctype="multipart/form-data">
            <?php
                //notification
                echo '<div class="notification">';
                if(isset($content['upload']['error']))
                {
                    echo $content['upload']['error'];
                }
                echo '</div>';
                foreach($content['list_label'] as $k => $v)
                {
                    echo '<div class="item-insert">';
                    echo '<h3>'.$v.'</h3>';
                    if($k=='id') //if id field
                    {
                        echo '<input class="ip-number" type="text" name="'.$k.'" value="'.(isset($content['data_field'][$k])?$content['data_field'][$k]:'').'" readonly />';
                    }
                    else if($k=='sort' || substr($k,0,6)=='volume') //if sort field or volume field
                    {
                        echo '<input class="ip-number" type="text" name="'.$k.'" value="'.(isset($content['data_field'][$k])?$content['data_field'][$k]:'1').'" />';
                    }
                    else if(substr($k,0,4)=='sold') //if sold field
                    {
                        echo '<input class="ip-number" type="text" name="'.$k.'" value="'.(isset($content['data_field'][$k])?$content['data_field'][$k]:'0').'" />';
                    }
                    else if(substr($k,0,5)=='price') //if price field
                    {
                         echo '<input class="ip-price" type="text" name="'.$k.'" value="'.(isset($content['data_field'][$k])?$content['data_field'][$k]:'0').'" />';
                    }
                    else if(substr($k,0,2)=='is') // if the field that contain 'is' prefix
                    {
                        //check update or insert
                        if(isset($content['data_field'][$k]))
                        {
                            if($content['data_field'][$k]==1)
                            {
                                echo '<input type="checkbox" name="'.$k.'" value="1" checked />';
                            }
                            else
                            {
                                echo '<input type="checkbox" name="'.$k.'" value="0" />';
                            }
                        }
                        else
                        {
                            echo '<input type="checkbox" name="'.$k.'" value="1" checked />';
                        }
                    }
                    else if(isset($content['list_textarea'][$k])) // check field is textarea, yes or no?
                    {
                        echo '<textarea name="'.$k.'" class="txtarea" rows="'.$content['list_textarea'][$k].'">'.(isset($content['data_field'][$k])?$content['data_field'][$k]:'').'</textarea>';
                    }
                    else if(isset($content['list_dropdown'][$k])) // check field is dropdown list, yes or no?
                    {
                        echo '<select class="sl-insert" name="'.$k.'">';
                        echo '<option value="0">-----list-----</option>';
                        foreach($content['list_dropdown'][$k] as $option)
                        {
                            //check update or insert
                            $op_text='';
                            if(isset($option['name']))
                            {
                                $op_text=$option['name'];
                            }
                            else if(isset($oppion['title']))
                            {
                                $op_text=$option['title'];
                            }
                            
                            if(isset($content['data_field'][$k]))
                            {
                                if($content['data_field'][$k]==$option['id'])
                                {
                                    echo '<option selected value="'.$option['id'].'">'.$op_text.'</option>';
                                }
                                else
                                {
                                    echo '<option value="'.$option['id'].'">'.$op_text.'</option>';
                                }
                            }
                            else
                            {
                                echo '<option value="'.$option['id'].'">'.$op_text.'</option>';
                            }
                        }
                        echo '</select>';
                    }
                    else if(substr($k,0,5)=='image') // check field is image, yes or no?
                    {
                        if(isset($content['data_field'][$k]))
                        {
                            echo '<img class="img-insert" src="'.base_url('data/images').'/'.$content['data_field'][$k].'" onerror="defaultimg(this)"/>';
                            echo '<br />';
                            echo '<input type="file" name="userfile" />';
                        }
                        else
                        {
                            echo '<input type="file" name="userfile" />';
                        }
                    }
                    else if(isset($content['ckeditor'][$k]))
                    {
                        echo '<textarea name="'.$k.'" id="'.$k.'">'.(isset($content['data_field'][$k])?$content['data_field'][$k]:'').'</textarea>';
                        echo display_ckeditor($content['ckeditor'][$k]);
                    }
                    else
                    {
                        if($k=='alias')
                        {
                            echo '<input class="ip-txt" type="text" name="'.$k.'" value="'.(isset($content['data_field'][$k])?$content['data_field'][$k]:'').'" readonly/>';
                        }
                        else
                        {
                             echo '<input class="ip-txt" type="text" name="'.$k.'" value="'.(isset($content['data_field'][$k])?$content['data_field'][$k]:'').'" />';
                        }
                    }
                    echo '</div>';
                }
            ?>
                <div class="insert-act">
                    <?php
                        if(isset($content['data_field']))
                        {
                            echo '<input type="submit" name="submit" value="Update" />';
                            echo '&nbsp;&nbsp;';
                            if($content['is_panel_area']==TRUE)
                            {
                                echo '<a class="lkbtn" href="'.site_url('dashboard/backend').'">Cancel</a>';
                            }
                            else
                            {
                                echo '<a class="lkbtn" href="'.site_url('dashboard/backend/view').'/'.$content['table'].'">Cancel</a>';
                            }
                        }
                        else
                        {
                            echo '<input type="submit" name="submit" value="Insert" />';
                            echo '&nbsp;&nbsp;';
                            echo '<input type="reset" name="reset" value="Reset"/>';
                        }
                    ?>     
                </div>
            </form>
       </div>
    <?php 
        endif;    
   ?>
</div>